
layui.use(['form', 'layer', 'laydate', 'table', 'upload'], function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        upload = layui.upload,
        table = layui.table;

    var datalist_url = $('.datalist_url').val();

    var delete_url = $('.delete_url').val();



    // Table 列表
    var tableIns = table.render({
        elem: '#dataTable',
        url : datalist_url,
        page : true,
        cellMinWidth : 95,
        //height : "full-100",
        limit : 10,
        limits : [10, 20, 50, 100],
        id : "dataTable",
        initSort: {
            field: 'currentPrice' // 排序字段，对应 cols 设定的各字段名
            ,type: 'asc' // 排序方式  asc: 升序、desc: 降序、null: 默认排序
        },
        cols : [[
            /* {type: "checkbox", fixed:"left", width:50},*/
            {field: 'key', title: '序号', width:80, align:"center"},
            {field: 'coupon_name', title: '优惠券'},
            {field: 'account_number', title: '会员号', align:'center'},
            {field: 'create_date', title: '领取时间', align:'center'},
            {field: 'coupon_purpose', title: '领取方式',  align:"center"},
            {field: 'status', title: '当前状态', align:'center'},
            {field: 'use_date', title: '使用时间', align:'center'},
        ]]
    });

    // 搜索
    $(".search_btn").on("click", function(){

        if ($('.title').val() != ''){
            var name = $('.searchBox').find('input[name=name]').val();
        }
        if ($('.account_number').val() != ''){
            var account_number = $('.searchBox').find('input[name=account_number]').val();
        }
        if ($('.coupon_purpose').val() != '') {
            var coupon_purpose = $('.coupon_purpose').val();
        }
        if ($('.time').val() != '') {
            var time = $('.time').val();
        }

        table.reload("dataTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                name: name,
                coupon_purpose: coupon_purpose,
                time: time,
                account_number: account_number,
            }
        });
    });

    // 批量删除
    $(".delAll_btn").on("click", function(){
        var checkStatus = table.checkStatus('tableList'),
            data = checkStatus.data,
            ids = [];
        if(data.length > 0) {
            for (var i in data) {
                ids.push(data[i].id);
            }
            layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    url: delete_url,
                    data: {"ids": ids},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('删除成功', {icon: 1});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            })
        }else{
            layer.msg("请选择需要删除的数据");
        }
    });


    // 列表操作
    table.on('tool(dataTable)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        // 删除
        if (layEvent === 'del') {
            layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
                $.ajax({
                    url: delete_url,
                    data: {"id": data.id},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('已删除', {icon: 1, time: 1000});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            });
        } else if(layEvent === 'edit'){

            formHtml(data);
        }
    });


});